//
//  BFCircleView.m
//  BrightField
//
//  Created by xiaomei su on 17/7/19.
//  Copyright © 2017年 xiaomei su. All rights reserved.
//

#import "BFCircleView.h"


@implementation BFCircleView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self colorSet];
        self.circleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        self.circleLabel.layer.cornerRadius = 20;
        self.circleLabel.layer.masksToBounds = YES;
        self.circleLabel.backgroundColor = [UIColor grayColor];
        [self addSubview:self.circleLabel];


    }
    return self;
}

-(void)colorSet{
    
    //创建背景圆环
    CAShapeLayer *trackLayer = [CAShapeLayer layer];
    trackLayer.frame = self.bounds;
    //清空填充色
    trackLayer.fillColor = [UIColor clearColor].CGColor;
    //设置画笔颜色 即圆环背景色
    trackLayer.strokeColor =  RGB(255, 255, 255, 0).CGColor;
    trackLayer.lineWidth = 5;
    //设置画笔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0) radius:self.frame.size.width/2.0 - 10 startAngle: M_PI/4*3 endAngle:M_PI/4*3+M_PI/4*3/20 clockwise:YES];
    //path 决定layer将被渲染成何种形状
    trackLayer.path = path.CGPath;
    [self.layer addSublayer:trackLayer];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    CALayer *gradientLayer = [CALayer layer];
    gradientLayer.frame = self.bounds;
    
    CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(0, 0, width*1,  height);
    gradientLayer1.colors = @[(__bridge id)RGB(0, 0, 0, 1).CGColor, (__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor blackColor].CGColor];
    //startPoint和endPoint属性，他们决定了渐变的方向。这两个参数是以单位坐标系进行的定义，所以左上角坐标是{0, 0}，右下角坐标是{1, 1}
    //startPoint和pointEnd 分别指定颜色变换的起始位置和结束位置.
    //当开始和结束的点的x值相同时, 颜色渐变的方向为纵向变化
    //当开始和结束的点的y值相同时, 颜色渐变的方向为横向变化
    //其余的 颜色沿着对角线方向变化
    gradientLayer1.startPoint = CGPointMake(0, 0);
    gradientLayer1.endPoint = CGPointMake(1,0);
    [gradientLayer addSublayer:gradientLayer1];
    [self.layer addSublayer:gradientLayer];
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor = [UIColor clearColor].CGColor;
    _progressLayer.strokeColor = [UIColor colorWithRed:170/255.0 green:210/255.0 blue:254/255.0 alpha:1].CGColor;
    _progressLayer.lineWidth = 10;
    _progressLayer.path = path.CGPath;
    gradientLayer.mask = _progressLayer;
    
    
    [self createBezierPath:self.frame];
}

//画两个圆形
-(void)createBezierPath:(CGRect)mybound
{
    
    //外圆
    self.greenTrackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:self.frame.size.width/2.0 startAngle:M_PI/4*3 endAngle:M_PI_4 clockwise:YES];;
    
    _greenTrackLayer = [CAShapeLayer new];
    [self.layer addSublayer:_greenTrackLayer];
    _greenTrackLayer.fillColor = [UIColor whiteColor].CGColor;
    _greenTrackLayer.strokeColor=[UIColor grayColor].CGColor;
    _greenTrackLayer.path = _greenTrackPath.CGPath;
    _greenTrackLayer.lineWidth=5;
    _greenTrackLayer.frame = mybound;
    
    //内圆
    self.greenProgressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:self.frame.size.width/2.0-10 startAngle:M_PI/4*3 endAngle:M_PI_4 clockwise:YES];
    
    
    
    _greenProgressLayer = [CAShapeLayer new];
    [self.layer addSublayer:_greenProgressLayer];
    _greenProgressLayer.fillColor = nil;
    _greenProgressLayer.strokeColor=[UIColor redColor].CGColor;
    _greenProgressLayer.lineCap = kCALineCapRound;
    _greenProgressLayer.path = _greenProgressPath.CGPath;
    _greenProgressLayer.lineWidth=5;
    _greenProgressLayer.frame = mybound;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    
    if (CGPathContainsPoint(path.CGPath, NULL, [touch locationInView:self], NO)	) {
        CGPoint touchCenter = [touch locationInView:self];
        CGFloat distanceX = 0.0;
        CGFloat distanceY = 0.0;
        if (touchCenter.x > 120) {
            distanceX = touchCenter.x - 120;
        }else{
            distanceX = 120 - touchCenter.x;
        }
        
        if (touchCenter.y > 120) {
            distanceY = touchCenter.y - 120;
        }else{
            distanceY = 120 - touchCenter.y;
        }
        CGFloat distance = sqrt(pow(distanceX, 2)+pow(distanceY, 2));
        CGFloat x = 0.0;
        CGFloat y = 0.0;
        if (touchCenter.x > 120) {
            x = 120 + distanceX * (100/distance);
        }else{
            x = 120 - distanceX * (100/distance);
        }
        if (touchCenter.y > 120) {
            y = 120 + distanceY * (100/distance);
        }else{
            y = 120 - distanceY * (100/distance);
        }
        
        if (distance > 80 && distance <
            120) {
            self.circleLabel.center = CGPointMake(x, y);

        }

//        self.circleLabel.center = [touch locationInView:self];
        
    }
    
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
//    CGFloat r = self.bounds.size.width / 2;
    
//    CGPoint center = CGPointMake(r, r);
    
    UITouch *touch = [touches anyObject];
    self.circleLabel.hidden = YES;
    CGPoint pointL = [touch locationInView:self];
    
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    
    if (CGPathContainsPoint(path.CGPath, NULL, [touch locationInView:self], NO)) {
        
//        self.circleLabel.center = [touch locationInView:self];
        // 圆心到圆外触摸点的距离
        CGFloat distanstWidth = 0.0;
        CGFloat distanceHeight = 0.0;
        
        if (pointL.x > 120) {
            distanstWidth = pointL.x - 120;
        }else{
            distanstWidth = 120 - pointL.x;
        }
        
        if (pointL.y > 120) {
            distanceHeight = pointL.y - 120;
        }else{
            distanceHeight = 120 -pointL.y;
        }
        
        
        CGFloat distance = sqrt(pow(distanstWidth, 2) + pow(distanceHeight, 2));
        NSLog(@"%f\n %f",pointL.x,pointL.y);
        CGFloat beishu = 100/distance;
        
        CGFloat x = 0.0;
        CGFloat y = 0.0;
        if (pointL.x > 120) {
            x = 120 + (pointL.x-120)*beishu;
        }else{
            x = 120 - (120 - pointL.x)*beishu;

        }
        if (pointL.y > 120) {
            y = 120 + (pointL.y-120)*beishu;
        }else{
            y = 120 - (120 - pointL.y)*beishu;
            
        }
        if (distance > 80 && distance <
            120) {
            self.circleLabel.center = CGPointMake(x, y);
            [self colorOfPoint:pointL];
            self.circleLabel.hidden = NO;


            
        }
        
        
    }else{
        [self colorOfPoint:pointL];
        // 圆心到圆外触摸点的距离
        CGFloat distanstWidth = 0.0;
        CGFloat distanceHeight = 0.0;
        
        if (pointL.x > 120) {
            distanstWidth = pointL.x - 120;
        }else{
            distanstWidth = 120 - pointL.x;
        }
        
        if (pointL.y > 120) {
            distanceHeight = pointL.y - 120;
        }else{
            distanceHeight = 120 -pointL.y;
        }
        
        
        CGFloat distance = sqrt(pow(distanstWidth, 2) + pow(distanceHeight, 2));
        NSLog(@"%f\n %f",pointL.x,pointL.y);
        CGFloat beishu = 100/distance;
        
        CGFloat x = 0.0;
        CGFloat y = 0.0;
        if (pointL.x > 120) {
            x = 120 + (pointL.x-120)*beishu;
        }else{
            x = 120 - (120-pointL.x)*beishu;
            
        }
        if (pointL.y > 120) {
            y = 120 + (pointL.y-120)*beishu;
        }else{
            y = 120 - (120-pointL.y)*beishu;
            
        }
        
        
        if (distance > 80 && distance <
            120) {
            self.circleLabel.center = CGPointMake(x, y);
            [self colorOfPoint:pointL];


            
        }
    }
    self.circleLabel.hidden = NO;

}

// 获取点上的颜色
- (UIColor *)colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
    
    CGContextTranslateCTM(context, -point.x, -point.y);
    
    [self.layer renderInContext:context];
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    NSLog(@"%d %d %d",pixel[0],pixel[1],pixel[2]);
    if (_delegate && [_delegate respondsToSelector:@selector(BFCircleViewTouchPoint:color:red:green:blue:alpha:)]) {
        [_delegate BFCircleViewTouchPoint:point color:color red:pixel[0] green:pixel[1] blue:pixel[2] alpha:pixel[3]/255.0];
    }
    
    return color;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    self.layer.cornerRadius = self.bounds.size.width / 2;
    self.layer.masksToBounds = YES;
//    self.layer.borderColor = [UIColor blackColor].CGColor;
//    self.layer.borderWidth = 1;
    self.circleLabel.center = CGPointMake(120, 20);
    
}

@end
